<template>
  <div class="order_list">
    <nav-bar title="历史订单">
      <!-- <span style="color: #333">
        <svg-icon icon-class="share" :width="15" :height="15" />
      </span> -->
    </nav-bar>
    <van-tabs v-model="activeIndex" :swipe-threshold="5" sticky offset-top="46" type="line" @click="handleTabClick">
      <van-tab v-for="(tabTitle, index) in tabTitles" :key="index" :title="tabTitle">
        <!-- <van-pull-refresh v-model="refreshing" style="min-height: calc(100vh - 84px)" @refresh="onRefresh"> -->
        <!-- <van-list v-model="loading" :finished="finished" :immediate-check="false"> -->
        <van-empty v-if="orderList.length==0" description="您还没有相关订单" />
        <Item
          v-for="(item, idx) in orderList"
          :key="idx + 10"
          :is-show-step="true"
          :index="item.orderCode?item.orderCode:item.orderId"
          :num="item.goodsCount"
          :thumb="item.payImgUrl"
          :title="item.goodsName"
          :sale-user="item.saleUserName"
          :buy-user="item.buyUserName"
          :price="item.orderPrice| yuan"
          :is-show-checkbox="false"
          :express="item.logisticsNumber"
          :disabled="item.saleStatus==3"
          :tags="!item.statusName ? [] : [item.statusName]"
          :time="item.createTime"
          tag-slot="warehouseTagSlot"
          class="warehouse-card"
          @input="handleItemSelect($event, 'purchase', idx, item.checked)"
        >
          <template name="warehouseTagSlot">
            <van-tag
              v-for="(item, idx) in !item.statusName ? [] : [item.statusName]"
              :key="idx"
              plain
              type="danger"
              style="margin-right: 4px"
              class="mt-12"
            >{{ item }}</van-tag>
          </template>
        </Item>
        <!-- </van-list> -->
        <!-- </van-pull-refresh> -->
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import {
  checkAddReq,
  getCargoPrice,
  getRepositoryData,
  getPurchaseProductList,
  settleCart,
  updatePayAuditRefuse,
  updateWarehouseStatus
} from '@/api/cart'
import {getAllCgOrderList} from '@/api/user'
import Item from '@/views/warehouse/modules/Item.vue'

export default {
  name: 'PreAllList',
  components: {
    Item
  },
  props: {
    active: {
      type: [String, Number],
      default: 0
    }
  },
  data() {
    return {
      activeIndex: Number(this.active),
      tabTitles: ['买仓', '卖仓'],
      orderList: [],
      refreshing: false,
      page: 0,
      limit: 10,
      loading: true,
      orderListEmptyShow: false,
      finished: false
    }
  },
  created() {
    this.init()
  },

  methods: {
    async  init() {
      this.page = 0
      this.orderList = []
      await this.getOrderList(true)
    },
    // 下拉刷新
    // onRefresh() {
    //   this.refreshing = true
    //   this.page = 0
    //   this.orderList = []
    //   this.getOrderList(true)
    // },
    async getOrderList(init) {
      this.loading = true
      this.orderListEmptyShow = false
      const res = await getAllCgOrderList({ type: this.activeIndex + 1 })
      let data=res.map.page.records
      data.forEach(item=>{
        let date =new Date(item.createTime)
        let y = date.getFullYear()
        let M = date.getMonth()>8 ?date.getMonth()+1 : '0'+(date.getMonth()+1)
        let d = date.getDate()>9 ? date.getDate(): '0'+date.getDate()
        let h = date.getHours()>9 ? date.getHours(): '0'+date.getHours()
        let m = date.getMinutes()>9 ? date.getMinutes(): '0'+date.getMinutes()
        let s = date.getSeconds()>9 ? date.getSeconds(): '0'+date.getSeconds()
        item.createTime = `${y}-${M}-${d} ${h}:${m}:${s}`
      })
      this.orderList= [...this.orderList,...res.map.page.records]
      this.loading = false
      if (init && res.map.page.records.length <= 0) {
        this.orderListEmptyShow = true
      }
    },
    async handleTabClick(e) {
      this.$set(this, 'orderList', [])
      if (this.activeIndex <= 1) {
        await this.getOrderList(true)
      } else {
        await this.getPreData()
      }
    },
    getPreData() {
      this.loading = true
      this.orderListEmptyShow = false
      getPurchaseProductList().then((res) => {
        // const { map: { records }} = res
        const list = res.map.list
        const orderList = list.reduce((result, item) => {
          item.goodsPrice = item.goodsPrice * item.goodsCount
          result.push(item)
          return result
        }, [])
        this.$set(this,'orderList',orderList)
        this.loading = false
        if (list.length <= 0) {
          this.orderListEmptyShow = true
        }
      })
    }
  }
}
</script>
<style lang="scss">
.order-list {
    .van-card {
        background: #fff;
        margin-top: 0;
    }

    .van-tabs__wrap {
        background: #fff;
        padding: 4px 0;
    }
}
</style>
<style lang="scss" scoped>
::v-deep .van-list {
    padding-bottom: 20px;
}

.desc {
    margin-top: 20px;
}

.order_list {
    background: #f5f5f5;
    min-height: 100vh;

    .order-item {
        background-color: #fff;
        margin: 15px 5px;
        border-radius: 15px;

        .order-title {
            display: flex;
            justify-content: space-between;
            padding: 25px 20px;

            .order-sn {
                text-align: left;
            }

            .order-staus {
                text-align: right;
            }
        }

        .van-card {
            background-color: #fff;
        }

        .total {
            text-align: right;
            padding: 10px;
        }

        .footer_btn {
            text-align: right;
            padding: 10px;

            .van-button {
                margin-left: 10px;
                min-width: 70px;
            }
        }
    }
}
.warehouse-card{
  margin-top: 10px;
  padding: 10px 0;
}
</style>
